// 回文链表的递归算法
// let pointer;

// function reverseLinkList(head) {
//     if (!head) {
//         return true
//     }
//     const res = reverseLinkList(head.next) && (pointer.val === head.val)
//     pointer = pointer.next;
//     return res;
// }

// function ispalindrome(head) {
//     pointer = head;
//     return reverseLinkList(head);
// }

// 快慢指针
function ispalindrome(head) {
    if (!head || !head.next) {
        return true;
    }
    let slowref = head;
    let fastref = head;
    let reverseref;
    let reversepreref;
    while (fastref && fastref.next) {
        fastref = fastref.next.next;
        reverseref = slowref;
        slowref = slowref.next;
        reverseref.next = reversepreref;
        reversepreref = reverseref;
    }
    if (fastref) {
        slowref = slowref.next;
    }
    while (reverseRef && slowRef) {
        // 链表逐个值比较
        if (reverseRef.val !== slowRef.val) {
            return false;
        }
        reverseRef = reverseRef.next;
        slowRef = slowRef.next;
    }
    return true;
}
head = [1, 9, 3, 2, 1];
console.log(ispalindrome(head));